﻿using Dispatcher.Core.Wechat.Models;
using MiniFox.Components;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Utility;
using Wechat.Core;
using Wechat.Corp;
using Wechat.Corp.Auth.Models;
using Wechat.Corp.Auth.Services;
using Wechat.Corp.Message.Models;
using Wechat.Corp.Message.Services;

namespace Dispatcher.Core.Wechat
{
    /// <summary>
    /// 
    /// </summary>
    public class WechatEngineService : EngineServiceBase
    {
        public WechatEngineService(SystemPrincipal principal) : base(principal)
        {

        }
        [AutoWired]
        public IWechatProvider Provider { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected virtual T CreateAgentService<T>(string name) where T : WechatAgentService
        {
            T _server = ComponentFactory.GetComponent<T>(name);
            return _server;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<WechatSendResponse> SendMessageAsync(WechatSendRequest request)
        {
            var wechatAgent = this.CreateAgentService<WechatAgentMessageService>(request.AccountName);
            request.Body = await GetBodyContentAsync(request);

            var users = GetWechatUsers(request.AccountName, request.Receiver);
            MessageResponse msgResponse;
            switch (request.MessageType)
            {
                case MessageType.Card:
                    msgResponse = await wechatAgent.SendTextCardMessage(request.Title, request.Body, request.Url, null, users);
                    break;
                case MessageType.News:
                    msgResponse = await wechatAgent.SendNewsMessage(request.Title, request.Body, request.Url, request.PictureUrl, null, null, users);
                    break;
                case MessageType.Markdown:
                    msgResponse = await wechatAgent.SendMarkdownMessage(request.Body, users);
                    break;
                case MessageType.Text:
                default:
                    msgResponse = await wechatAgent.SendTextMessage(request.Body, users);
                    break;
            }
            WechatSendResponse response = new()
            {
                MessageId = msgResponse.MessageId,
                SendTime = DateTime.Now,
                OrignalResponse = msgResponse.ToJsonString()
            };
            this.Provider.SaveMessageResult(request, response);

            return response;
        }

        private string[] GetWechatUsers(string name, string[] users)
        {
            var WechatAgent = this.CreateAgentService<WechatAgentAuthService>(name);
            var wechatUsers = this.GetOpenAuthUser<UserInfoResponse>(ApiGlobalKey.USER_AGENT_NAME, users);
            var userList = wechatUsers.Select(u => u.UserID);
            return userList.ToArray();
        }
    }
}
